<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信登录跳转页</title>
</head>
<body>
<script src="/js/jquery.min.js"></script>
<script src="/js/constant.js"></script>
<script>

    function getQueryVariable(variable)
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    $.ajax({
        type: "get",
        url:  "/weixin/system-info",
        success: function(r) {
            if (r.code == 0) {
                getUserInfo(r.data);
            } else {
                location.href = '/login';
            }
        }
    });

    function getUserInfo(d) {
        const code = getQueryVariable("code");
        const state = getQueryVariable("state");
        $.ajax({
            type: "post",
            url: xxzzu.host + "/wechat/user-info",
            data: {
                "authorizeCode": code,
                "state": state,
                "appId": d.appId,
                "bizId": d.bizId
            },
            success: function(r) {
                if (r.code == 0) {
                    login(r.data);
                } else {
                    location.href = '/login';
                }
            }
        });
    }

    function login(userInfo) {
        $.ajax({
            type: "post",
            contentType: "application/json; charset=utf-8",
            url: "/weixin/login",
            data:  JSON.stringify(userInfo),
            success: function(r) {
                if (r.code == 0) {
                    location.href = '/index';
                } else {
                    location.href = '/login';
                }
            }
        });
    }

</script>
</body>
</html>
